package ysu.sxm.ptjc.Service.Impl;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import ysu.sxm.ptjc.Dao.MonthStaticsDao;
import ysu.sxm.ptjc.Domin.MonthModel;
import ysu.sxm.ptjc.Domin.ProduceInfoMondel;
import ysu.sxm.ptjc.Service.MonthStaticsService;
import ysu.sxm.ptjc.pojo.ProduceStatisticalInfo;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Service
public class MonthStaticsServiceImpl implements MonthStaticsService {
    @Autowired
    MonthStaticsDao monthStaticsDao;
    @Override
    public String monthStatistic(MonthModel model, Map<String,Map<String, Map<String, ProduceStatisticalInfo>>> res){
        List<ProduceInfoMondel> proNumAndCarType=monthStaticsDao.getProNumAndCarType(model);//获取车型及对应的produce_num
        Map<String,List<String>> carTypeToproNum=new LinkedHashMap<>();
        /**
         * 处理车型与produce_num的对应关系
         * 一个车型对应多个produce_num
         */
        for (ProduceInfoMondel p:proNumAndCarType
             ) {
            if(carTypeToproNum.containsKey(p.getProduceName()))
            {
                carTypeToproNum.get(p.getProduceName()).add(p.getProduceNum());
            }else{
                List<String> pro=new ArrayList<>();
                pro.add(p.getProduceNum());
                carTypeToproNum.put(p.getProduceName(),pro);
            }
        }
        //判断是否查到车型和对应produce_num
        if(proNumAndCarType!=null&&proNumAndCarType.size()!=0){
         /**
         * 如果前端传来的车型为空，那么将车型填入结果中
         */
            if(res.size()==0){
                for (ProduceInfoMondel p:proNumAndCarType) {
                    Object o=new Object();
                    res.put(p.getProduceName(), (Map<String, Map<String, ProduceStatisticalInfo>>) o);
                }
            }
            /**
             * 某个车型对应的产品类型的数据查询
             */
            carTypeToproNum.forEach((key,value)->{
                //获得对应车型的produce_num
                List<ProduceInfoMondel> produceNumAndProduceName=monthStaticsDao.getLimitProNumAndProduceName(value);
                if(produceNumAndProduceName!=null&&produceNumAndProduceName.size()!=0){
                    Map<String, Map<String, ProduceStatisticalInfo>> monthAndNameMap=new LinkedHashMap<>();
                    /**
                     * 获得对应产品名称的统计情况，主要解决的问题是如何做到返回的list与产品名称对应暂时没有想到好的办法
                     * 决定采用数据库中pro_statics_info表中的产品名称
                     */
                    List<ProduceStatisticalInfo> list=monthStaticsDao.monthStatics(produceNumAndProduceName);
                    if(list!=null&&!list.isEmpty()){
                        for (ProduceStatisticalInfo info: list
                             ) {
                            if(!monthAndNameMap.containsKey(info.getProduceDate().getMonth()+1)){
                                System.out.println("获得的月份为"+info.getProduceDate().getMonth());
                                Map<String, ProduceStatisticalInfo> infoMap=new LinkedHashMap<>();
                                infoMap.put(info.getProduceName(),info);
                                monthAndNameMap.put(String.valueOf(info.getProduceDate().getMonth()+1),infoMap);
                            }else{
                                System.out.println("获得的月份为"+info.getProduceDate().getMonth());
                                monthAndNameMap.get(info.getProduceDate().getMonth()+1).put(info.getProduceName(),info);
                            }
                        }
                    }
                    res.put(key,monthAndNameMap);
                }
            });
        }
        return JSON.toJSONString(res);
    }
}
